class GetInput(object):
    def __init__(self):
        self.qdict = {}

    def add(self, question, answer):
        self.qdict[question] = answer

    def ask(self, question, atype, akey=None):
        """ Takes a question to ask and response type.  Returns an answer. """
        self.question = question
        self.atype = atype
        self.akey = akey
        try:
            print ''
            print question
            answer = raw_input('>')
        except KeyboardInterrupt:
            print 'Input not recognized. Input valid response or type \'q\' to quit.'
            self.ask(self.question, self.atype, self.akey)
        else:
            if answer == 'q': sys.exit()
            if self.akey and answer not in self.akey:
                print 'Response not in accepted answer list.'
                self.ask(self.question, self.type, self.akey)
            self.qdict[question] = answer
            return (self._binary(answer) if atype == 'binary' else
                    self._fname(answer) if atype == 'file' else
                    self._multi(answer, akey) if atype == 'multi' else
                    self._text(answer) if atype == 'text' else
                    'Response unavailable.')

    def _multi(self, answer, akey):
        """ Ask the user a multiple choice question. """
        if answer not in akey:
            print 'Response is not acceptable.'
            print ''
            return self.ask(self.question, 'multi', akey)
        return answer


    def _binary(self, answer):
        """ Ask the user a question with a yes or no response.
            Store the question and answer in self.qdict. """
        pos = ['Y', 'y', 'YES', 'yes', 'Yes']
        neg = ['N', 'n', 'NO', 'No', 'no']
        if answer in pos: return True
        elif answer in neg: return False
        else:
            print 'Response not in accepted answer list.'
            print ''
            return self.ask(self.question, 'binary')

    def _fname(self, answer):
        """ Ask the user a question where the answer is a filename.
            Verify the filename exists.
            Store the question and answer in self.qdict. """
        if '.csv' not in answer:
            print 'Only CSV files are accepted.'
            print ''
            return self.ask(self.question, 'file')
        elif not os.path.exists(answer):
            print 'File not found.'
            print ''
            return self.ask(self.question, 'file')
        else: return answer

    def _text(self, answer):
        """ Return raw text. """
        return answer

# add class to deal with configuration files.
class ConfigFile(object):

    def read(self):
        try:
            print os.getcwd()
            with open('scan_config.txt', 'rU') as f:
                self.cfile = f.readlines()
        except:
            option = responses.ask('Unable to open config file - (r)efresh, (q)uit or (c)ontinue?', 'multi',
                                   {'refresh': 'refresh', 'r': 'refresh', 'quit': 'quit', 'q': 'quit', 'continue': 'continue', 'c': 'continue'})
            if option == 'quit': sys.exit()
            return self.read() if option == 'refresh' else None
        else:
            print 'File read successfully.'

    def write(self):
        pass

    def parse(self):
        _config = {line.split('=')[0].strip(): line.split('=')[1].strip() for line in self.cfile}
        print 'Configuration found:\n'
        print '\n'.join([str(item) for item in _config.items()])
        load = responses.ask('Load this configuration? (y/n)> ', 'binary')
        if load: self.config = _config